package com.tsky.yyj.action;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.tsky.casing.QueryBean;
import com.tsky.casing.action.BackAction;
import com.tsky.common.ActionUtil;
import com.tsky.common.BeanUtil;
import com.tsky.yyj.entity.Classify;
import com.tsky.yyj.json.YYJJsonFormat;
import com.tsky.yyj.manager.BaseManager;

@Component("classifyAction")
@Scope("prototype")
public class ClassifyAction extends BackAction {
	@Autowired
	private BaseManager entityManager;

	/* bean properties */
	private String id;
	private String ids;
	private String name;
	private String pid;
	private String property;
	private String initid;

	public void grid() {
		QueryBean grid = new QueryBean();
		BeanUtil.copyProperties(grid, this);

		entityManager.query(grid, Classify.class);

		String jsonData = YYJJsonFormat.classifyToJson(grid);
		ActionUtil.ajaxWrite(jsonData);
	}

	public void add() {
		Classify entity = new Classify();
		BeanUtil.copyProperties(entity, this);
		entity.setId(BeanUtil.getID());
		
		// 父菜单
		Classify pMenu = new Classify();
		if(!BeanUtil.isEmpty(pid)){
			pMenu = entityManager.getEntity(Classify.class, pid);
			entity.setParent(pMenu);
			entity.setProperty("0");
		}else {
			entity.setProperty("1");
		}

		boolean result = entityManager.saveEntity(entity);
		ActionUtil.ajaxWrite(result);
	}

	public void delete() {
		String[] idset = ids.split(",");
		boolean result = entityManager.delEntity(Classify.class, idset);
		ActionUtil.ajaxWrite(result);
	}

	public String query() {
		Classify entity = entityManager.getEntity(Classify.class, id);
		ActionUtil.setToEL("classifyEnt", entity);
		return "query";
	}

	public void update() {
		Classify entity = entityManager.getEntity(Classify.class, id);
		BeanUtil.copyProperties(entity, this);
		
		// 父菜单
		Classify pMenu = new Classify();
		if(!BeanUtil.isEmpty(pid)){
			pMenu = entityManager.getEntity(Classify.class, pid);
			entity.setParent(pMenu);
			entity.setProperty("0");
		}else {
			entity.setProperty("1");
			entity.setParent(null);
		}

		boolean result = entityManager.updateEntity(entity);
		ActionUtil.ajaxWrite(result);
	}

	public BaseManager getEntityManager() {
		return entityManager;
	}

	public void setEntityManager(BaseManager entityManager) {
		this.entityManager = entityManager;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPid() {
		return pid;
	}

	public String getProperty() {
		return property;
	}

	public void setProperty(String property) {
		this.property = property;
	}

	public void setPid(String pid) {
		this.pid = pid;
	}

	public String getInitid() {
		return initid;
	}

	public void setInitid(String initid) {
		this.initid = initid;
	}

}
